package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/7/8.<br><br>
 *
 * 各位相加
 */
public class Topic258 {

    public static void main(String[] args) {
        Topic258 instance = new Topic258();

        System.out.println(instance.addDigits(0));
        System.out.println(instance.addDigits(38));
        System.out.println(instance.addDigits(45));
        System.out.println(instance.addDigits(154));
    }

    public int addDigits(int num) {
        return 1 + (num - 1) % 9;
    }

    public int addDigits2(int num) {
        if(num == 0) {
            return 0;
        }

        int result = split(num);
        while (result > 9) {
            result = split(result);
        }

        return result;
    }

    private int split(int num) {
        if(num < 10) {
            return num;
        }

        int sum = 0;
        while (num != 0) {
            sum += num % 10;
            num = num / 10;
        }

        return sum;
    }
}
